package com.wt;

import com.wt.bean.ConsumerComponent;
import com.wt.configuration.ConsumerConfiguration;
import com.wt.service.SpIService;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: wtt
 * @Date: 2022/1/15 12:22
 * @Description:
 */
public class ConsumerMain {

    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        applicationContext.start();

        ConsumerComponent consumerComponent = applicationContext.getBean(ConsumerComponent.class);

        // dubbo spi
        ExtensionLoader<SpIService> extensionLoader = ExtensionLoader.getExtensionLoader(SpIService.class);
        Set<String> supportedExtensions = extensionLoader.getSupportedExtensions();
        for (String extension : supportedExtensions) {
            String Result = extensionLoader.getExtension(extension).say();
            System.out.println("spi:" + Result);
        }
        // dubbo spi adaptive
        URL url = URL.valueOf("test://localhost/hello?sp.i.service=");
        SpIService spIService = ExtensionLoader.getExtensionLoader(SpIService.class).getAdaptiveExtension();
        String say = spIService.say(url);
        System.out.println(say);

        while (true) {
            for (int i = 0; i < 1000; i++) {
//                TimeUnit.SECONDS.sleep(2);
                new Thread(() -> {
                    String result = consumerComponent.sayHello("张三");
                    System.out.println("result:" + result);
                }).start();
            }
//            System.in.read();
        }

    }
}
